using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.SceneManagement;

public class Leveling : MonoBehaviour
{


    //人物现血
    long lead_residual_blood_volume;
    //boos现血
    long boos_residual_blood_volume;
    //随机数，攻击力
    int attack_Power;
    //随机数，boos加血量
    int boos_upgrade_blood_volume;
    //随机数，属性1
    int attribute1Is;
    //随机数，属性2
    int attribute2Is;
    //随机数，属性值1
    int attribute1VIs;
    //随机数，属性2
    int attribute2VIs;



    //人物血图控件
    public Slider lead_blood_volume_slider;
    //boos血图控件
    public Slider boos_blood_volume_slider;
    //我方人马pancel
    public GameObject our_panel;
    //我方1，2，3，4
    public GameObject our1;
    //刀光控件
    public GameObject dao;
    //小怪们panel
    public GameObject other_panel;
    //小怪1，2，3，4
    public GameObject other1;
    
    ////掉血pancel
    //public GameObject dropping_blood_panel;
    ////掉血百位控件
    //public GameObject hundred;
    ////掉血十位控件
    //public GameObject decade;
    ////掉血个位控件
    //public GameObject single_digit;
    //结束panel
    public GameObject end_panel;
    //成功失败背景
    public GameObject image_over;
    //属性1控件
    public GameObject attribute1;
    //属性1增值控件
    public GameObject attribute1_value;
    //属性2控件
    public GameObject attribute2;
    //属性2增值控件
    public GameObject attribute2_value;
    //确定按钮控件
    public GameObject sureButton;

    float our_panel_x;
    float other_panel_x;
	float dropping_blood_panel_y;

    public Animator ani1;
    public Animator ani2;

    int a;
    int b = 0;

    void Start()
    {
        attribute1Is = Random.Range(0, 4);
        attribute2Is = Random.Range(0, 4);
        attribute1VIs = Random.Range(10, 50);
        attribute2VIs = Random.Range(10, 50);

        our_panel_x = our_panel.GetComponent<Transform>().position.x;
        other_panel_x = other_panel.GetComponent<Transform>().position.x;
		//dropping_blood_panel_y = dropping_blood_panel.GetComponent<Transform>().position.y;

		lead_residual_blood_volume = VariableDocument.GetInstance.lead_blood_volume();
        boos_residual_blood_volume = VariableDocument.GetInstance.boos_blood_volume;
        lead_blood_volume_slider.value = lead_residual_blood_volume * 1.0f / VariableDocument.GetInstance.lead_blood_volume();
        boos_blood_volume_slider.value = boos_residual_blood_volume * 1.0f / VariableDocument.GetInstance.boos_blood_volume;
        


    }

    // Update is called once per frame
    void FixedUpdate()
    {

        attack_Power = Random.Range((int)(VariableDocument.GetInstance.lead_blood_volume() * 0.1f), (int)(VariableDocument.GetInstance.lead_blood_volume() * 0.5f));
        //Debug.Log(a);
        lead_blood_volume_slider.value = lead_residual_blood_volume * 1.0f / VariableDocument.GetInstance.lead_blood_volume();
        boos_blood_volume_slider.value = boos_residual_blood_volume * 1.0f / VariableDocument.GetInstance.boos_blood_volume;
        if(lead_residual_blood_volume > 0 && boos_residual_blood_volume > 0)
        { 
            if (a == 0)
            {
           
				Invoke("lead_attack", 1f);

            }
            if(a == 1)
            {
				//1秒后返回
				Invoke("lead_pause", 1f);
            }
            if (a == 2)
            {
				Invoke("boos_attack", 1f);
            }
            if (a == 3)
            {
				//1秒后返回
				Invoke("boos_pause", 1f);
            }
        }
        else if(lead_residual_blood_volume > 0 && boos_residual_blood_volume <= 0)
        {
			Invoke("lead_pause2", 1f);
			
   //         other_panel.SetActive(false);

			////成功
			//Debug.Log("成功");
   //         if(b == 0)
   //         {
			//	VariableDocument.GetInstance.boos_blood_volume += 100;
   //             end_panel.SetActive(true);
   //             image_over.GetComponent<Image>().sprite = Resources.Load<Sprite>("Leveling/组-37");
   //             sureButton.GetComponent<Image>().sprite = Resources.Load<Sprite>("Leveling/组-36");
   //             //SceneManager.LoadScene("Leveling");
   //             attribute1.GetComponent<Text>().text = VariableDocument.GetInstance.attribute[attribute1Is];
   //             attribute2.GetComponent<Text>().text = VariableDocument.GetInstance.attribute[attribute2Is];
   //             attribute1_value.GetComponent<Text>().text = "+" + attribute1VIs.ToString();
   //             attribute2_value.GetComponent<Text>().text = "+" + attribute2VIs.ToString();

   //             VariableDocument.GetInstance.attribute_value[attribute1Is] += attribute1VIs;
   //             VariableDocument.GetInstance.attribute_value[attribute2Is] += attribute2VIs;
   //             VariableDocument.GetInstance.boos_grade += 1;
   //             b = 1;
   //         }
            
        }
        else
        {
			Invoke("boos_pause2", 1f);
			//our_panel.SetActive(false);
			////失败
			//Debug.Log("失败");
   //         if (b == 0)
   //         {
   //             end_panel.SetActive(true);
   //             image_over.GetComponent<Image>().sprite = Resources.Load<Sprite>("Leveling/组-35");
   //             sureButton.GetComponent<Image>().sprite = Resources.Load<Sprite>("Leveling/色相_饱和度-32");
   //             attribute1.SetActive(false); 
   //             //属性1增值控件
   //             attribute1_value.SetActive(false);
   //             //属性2控件
   //             attribute2.SetActive(false);
   //             //属性2增值控件
   //             attribute2_value.SetActive(false);
   //             b = 1;
   //         }                
        }

    }
    void lead_attack()
    {
        //前往攻击
        
        if (our_panel.GetComponent<Transform>().position.x <= other_panel_x - 300 && a == 0)
        {
            ani1.SetBool("ToAttack", true);
            ani1.SetBool("GoBack", false);
            ani1.SetBool("ReBegin", false);
            ani1.SetBool("Attack", false);

           
            our_panel.GetComponent<Transform>().Translate(new Vector3(1, 0, 0));
            if (our_panel.GetComponent<Transform>().position.x >= other_panel_x - 300 && a == 0)
            {
                ani2.SetBool("b", true);
                ani1.SetBool("ToAttack", false);
                ani1.SetBool("Attack", true);
                dao.SetActive(true);
                a = 1;
                //dropping_blood_panel.SetActive(true);
				boos_residual_blood_volume -= attack_Power;
				
			}
			
			
		}
	}

    void lead_pause()
    {
        ani2.SetBool("b", false);

        //dropping_blood_panel.SetActive(false);
        if (our_panel.GetComponent<Transform>().position.x > our_panel_x )
        {
            ani1.SetBool("GoBack", true);
            ani1.SetBool("Attack", false);
            dao.SetActive(false);
            our_panel.GetComponent<Transform>().Translate(new Vector3(-20, 0, 0));
            if (our_panel.GetComponent<Transform>().position.x - our_panel_x <= 0 && a == 1)
            {
                ani1.SetBool("GoBack", false);
                ani1.SetBool("ReBegin", true);

                a = 2;
				//dropping_blood_panel.GetComponent<Transform>().position.y = dropping_blood_panel_y;
			}
            //Debug.Log("aaaa" + our_panel.GetComponent<Transform>().position.x);
            //Debug.Log("bbbb"+our_panel_x);
        }
    }
	void lead_pause2()
	{
        other_panel.SetActive(false);

        //成功
        Debug.Log("成功");
        if (b == 0)
        {
            VariableDocument.GetInstance.boos_blood_volume += 100;
            end_panel.SetActive(true);
            image_over.GetComponent<Image>().sprite = Resources.Load<Sprite>("Leveling/组-37");
            sureButton.GetComponent<Image>().sprite = Resources.Load<Sprite>("Leveling/组-36");
            //SceneManager.LoadScene("Leveling");
            attribute1.GetComponent<Text>().text = VariableDocument.GetInstance.attribute[attribute1Is];
            attribute2.GetComponent<Text>().text = VariableDocument.GetInstance.attribute[attribute2Is];
            attribute1_value.GetComponent<Text>().text = "+" + attribute1VIs.ToString();
            attribute2_value.GetComponent<Text>().text = "+" + attribute2VIs.ToString();

            VariableDocument.GetInstance.attribute_value[attribute1Is] += attribute1VIs;
            VariableDocument.GetInstance.attribute_value[attribute2Is] += attribute2VIs;
            VariableDocument.GetInstance.boos_grade += 1;
            b = 1;
        }

        //dropping_blood_panel.SetActive(false);
        if (our_panel.GetComponent<Transform>().position.x > our_panel_x)
		{
			ani1.SetBool("GoBack", true);
			ani1.SetBool("Attack", false);
			dao.SetActive(false);
			our_panel.GetComponent<Transform>().Translate(new Vector3(-20, 0, 0));
			if (our_panel.GetComponent<Transform>().position.x - our_panel_x <= 0 && a == 1)
			{
				ani1.SetBool("GoBack", false);
				ani1.SetBool("ReBegin", true);

				//a = 2;
				//dropping_blood_panel.GetComponent<Transform>().position.y = dropping_blood_panel_y;
			}
			//Debug.Log("aaaa" + our_panel.GetComponent<Transform>().position.x);
			//Debug.Log("bbbb"+our_panel_x);
		}
	}
	void boos_attack()
    {

        //前往攻击
        if (other_panel.GetComponent<Transform>().position.x >= our_panel_x + 250)
        {
            ani2.SetBool("toboss2", true);
            ani2.SetBool("toboss1", false);
            other_panel.GetComponent<Transform>().Translate(new Vector3(-1, 0, 0));
            if (other_panel.GetComponent<Transform>().position.x <= our_panel_x + 250 && a ==2)
            {
                ani2.SetBool("toboss2", false);
                ani2.SetBool("toboss4", true);
                ani1.SetBool("a",true);

                a = 3;
                //dropping_blood_panel.SetActive(true);
                lead_residual_blood_volume -= attack_Power;
            }
        }
        //Debug.Log("aaaa" + other_panel.GetComponent<Transform>().position.x);

    }
    void boos_pause()
    {
        ani1.SetBool("a", false);
        //dropping_blood_panel.SetActive(false);
        if (other_panel.GetComponent<Transform>().position.x < other_panel_x)
        {
            ani2.SetBool("toboss2", false);
            ani2.SetBool("move", true);
            ani2.SetBool("toboss4", false);
            other_panel.GetComponent<Transform>().Translate(new Vector3(20, 0, 0));
            if (other_panel.GetComponent<Transform>().position.x - other_panel_x >= 0)
            {
                ani2.SetBool("move", false);
                ani2.SetBool("toboss1", true);
                a = 0;
			
			}
        }
    }
	void boos_pause2()
	{
        our_panel.SetActive(false);
        //失败
        Debug.Log("失败");
        if (b == 0)
        {
            end_panel.SetActive(true);
            image_over.GetComponent<Image>().sprite = Resources.Load<Sprite>("Leveling/组-35");
            sureButton.GetComponent<Image>().sprite = Resources.Load<Sprite>("Leveling/色相_饱和度-32");
            attribute1.SetActive(false);
            //属性1增值控件
            attribute1_value.SetActive(false);
            //属性2控件
            attribute2.SetActive(false);
            //属性2增值控件
            attribute2_value.SetActive(false);
            b = 1;
        }
        //dropping_blood_panel.SetActive(false);
        if (other_panel.GetComponent<Transform>().position.x < other_panel_x)
		{
			ani2.SetBool("toboss2", false);
			ani2.SetBool("move", true);
			ani2.SetBool("toboss4", false);
			other_panel.GetComponent<Transform>().Translate(new Vector3(20, 0, 0));
			if (other_panel.GetComponent<Transform>().position.x - other_panel_x >= 0)
			{
				ani2.SetBool("move", false);
				ani2.SetBool("toboss1", true);
				//a = 0;

			}
		}
	}

	public void continue_Leveling()
    {
        SceneManager.LoadScene("Leveling");
    }
    public void goto_main()
    {
        SceneManager.LoadScene("MainGame");
    }
}